package br.com.view;

import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import br.com.beans.CPUList;

public class GradeCPU extends GridPane {

	private Label labelRetornoId;
	private Label labelId;
	private Label labelRetornoFabricante;
	private Label labelFabricante;
	private Label labelRetornoModelo;
	private Label labelModelo;
	private Label labelRetornoIP;
	private Label labelIP;
	private Label labelRetornoPartNumber;
	private Label labelPartNumber;
	private static Arvore arvore;
	private CPUList cpus;

	static int r = 0;
	static int c = 0;

	public GradeCPU() {

		if (arvore == null) {
			arvore = new Arvore();
			cpus = arvore.getCpu();

			this.setHgap(3);
			this.setVgap(3);
			this.setPadding(new Insets(3));
			this.setAlignment(Pos.CENTER_LEFT);

			c = 0;
			r = 1;
			labelId = new Label("Id: ");
			GridPane.setConstraints(labelId, c, r);

			c = 1;
			r = 1;
			labelRetornoId = new Label();
			GridPane.setConstraints(labelRetornoId, c, r);

			c = 0;
			r = 2;
			labelFabricante = new Label("Fabricante: ");
			GridPane.setConstraints(labelFabricante, c, r);

			c = 1;
			r = 2;
			labelRetornoFabricante = new Label();
			GridPane.setConstraints(labelRetornoFabricante, c, r);

			c = 0;
			r = 3;
			labelModelo = new Label("Modelo: ");
			GridPane.setConstraints(labelModelo, c, r);

			c = 1;
			r = 3;
			labelRetornoModelo = new Label();
			GridPane.setConstraints(labelRetornoModelo, c, r);

			c = 0;
			r = 4;
			labelIP = new Label("IP: ");
			GridPane.setConstraints(labelIP, c, r);

			c = 1;
			r = 4;
			labelRetornoIP = new Label();
			GridPane.setConstraints(labelRetornoIP, c, r);

			c = 0;
			r = 5;
			labelPartNumber = new Label("Part Number: ");
			GridPane.setConstraints(labelPartNumber, c, r);

			c = 1;
			r = 5;
			labelRetornoPartNumber = new Label();
			GridPane.setConstraints(labelRetornoPartNumber, c, r);
		}

		this.getChildren().addAll(labelId, labelRetornoId, labelFabricante,
				labelRetornoFabricante, labelModelo, labelRetornoModelo, labelIP,
				labelRetornoIP, labelPartNumber, labelRetornoPartNumber);

		StringProperty textoSelecionadoNaArvore = arvore.textoCPUselecionadaProperty();
		textoSelecionadoNaArvore.addListener(new ChangeListener<String>() {

			@Override
			public void changed(ObservableValue<? extends String> observable,
					String oldValue, String newValue) {

				System.out.println("GradeCPU >> "
						+ newValue);

				GradeCPU.this.labelRetornoId.setText(newValue);

				GradeCPU.this.labelRetornoFabricante.setText(cpus
						.getListaFabricanteCpus().get(observable.getValue()));

				GradeCPU.this.labelRetornoIP.setText(cpus.getListaIPCpus().get(
						observable.getValue()));

				GradeCPU.this.labelRetornoModelo.setText(cpus.getListaModeloCpus().get(
						observable.getValue()));

				GradeCPU.this.labelRetornoPartNumber.setText(cpus
						.getListaPartNumberCpus().get(observable.getValue()));

			}
		});

	}
}
